<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 内容卡片 -->
        <el-card>
            <el-row>
                <el-button type="primary">添加角色</el-button>
            </el-row>
            <el-row>
                <el-table :data="rolesLists" width="100%" border stripe>
                    <el-table-column type="expand">
                        <template slot-scope="scope">
                            <el-row :class="['bbottom', index1 === 0?'btop':'']" v-for="(item1, index1) in scope.row.children" :key="item1.id">
                                <el-col :span="5">
                                    <el-tag closable @close="removeRightsById(scope.row, item1.id)">{{item1.authName}}</el-tag>
                                    <i class="el-icon-caret-right"></i>
                                </el-col>
                                <el-col :span="19">
                                    <el-row :class="index2 === 0 ? '' : 'btop'" v-for="(item2, index2) in item1.children" :key="item2.id">
                                        <el-col :span="6">
                                            <el-tag type="success" closable @close="removeRightsById(scope.row, item2.id)">{{item2.authName}}</el-tag>
                                            <i class="el-icon-caret-right"></i>
                                        </el-col>
                                        <el-col :span="18">
                                            <el-tag v-for="item3 in item2.children" :key="item3.id" type="warning" closable @close="removeRightsById(scope.row, item3.id)">{{item3.authName}}</el-tag>
                                        </el-col>
                                    </el-row>
                                </el-col>
                            </el-row>
                             <pre>{{scope.row}}</pre>
                        </template>
                    </el-table-column>
                    <el-table-column type="index" label="#"></el-table-column>
                    <el-table-column prop="roleName" label="角色名称"></el-table-column>
                    <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
                            <el-button size="mini" type="warning" icon="el-icon-setting" @click="setRightsTree(scope.row)">分配权限</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-row>
        </el-card>
        <!-- 分配权限的弹窗 -->
        <el-dialog title="分配权限" :visible.sync="setRightsDialogVisible" width="50%" @close="setRightsDialogClose">
            <el-tree :data="rightsData" :props="defaultProps" show-checkbox node-key="id" :default-checked-keys="checkedRights" :default-expand-all="true" ref="setRightsRef"></el-tree>
            <span slot="footer" class="dialog-footer">
                <el-button @click="setRightsDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="allotRights">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    created() {
        this.getRolesLists()
    },
    data() {
        return {
            rolesLists: [],
            setRightsDialogVisible: false,
            rightsData: [],
            defaultProps: {
                children: 'children',
                label: 'authName'
            },
            checkedRights: [],
            roleId: ''
        }
    },
    methods: {
        // 获取权限列表
        async getRolesLists() {
            const { data: res } = await this.$http.get('roles')
            if (res.meta.status !== 200) return this.$message.error('获取角色列表数据失败')
            this.rolesLists = res.data
        },
        // 移除权限
        removeRightsById(role, id) {
            this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async() => {
                const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${id}`)
                if (res.meta.status !== 200) return this.$message.error('操作失败，请重试')
                role.children = res.data
                this.$message.success('删除权限成功！')
            }).catch(() => {
                this.$message.info('已取消删除')
            })
        },
        // 设置权限树对话框
        async setRightsTree(role) {
            this.roleId = role.id
            const { data: res } = await this.$http.get('rights/tree')
            this.rightsData = res.data
            this.getLeafKeys(role, this.checkedRights)
            this.setRightsDialogVisible = true
        },
        // 递归三级节点的id
        getLeafKeys(node, arr) {
            if (!node.children) {
                return arr.push(node.id)
            }
            node.children.forEach(item => {
                this.getLeafKeys(item, arr)
            })
        },
        // 设置关闭权限设置对话框后事件
        setRightsDialogClose() {
            this.checkedRights = []
            this.roleId = ''
        },
        // 分配权限
        async allotRights() {
            const keys = [...this.$refs.setRightsRef.getCheckedKeys(), ...this.$refs.setRightsRef.getHalfCheckedKeys()]
            const keysStr = keys.join(',')
            const { data: res } = await this.$http.post(`roles/${this.roleId}/rights`, { rids: keysStr })
            if (res.meta.status !== 200) return this.$message.error('设置权限失败')
            this.$message.success('设置权限成功')
            this.setRightsDialogVisible = false
            this.getRolesLists()
        }
    }
}
</script>

<style lang="less" scoped>
    .el-tag {
        margin: 7px;
    }

    .el-row {
        display: flex;
        align-items: center;
    }

    .btop {
        border-top: 1px solid #eee;
    }

    .bbottom {
        border-bottom: 1px solid #eee;
    }
</style>